package com.xmwzw.catpu;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;

import com.xmwzw.catpu.databinding.ActivityPwdChangeBinding;
import com.xmwzw.catpu.http.BaseRequest;
import com.xmwzw.catpu.http.bean.BaseRespond;
import com.xmwzw.catpu.http.listener.RegisterListener;
import com.xmwzw.catpu.util.ActivityManagerUtils;
import com.xmwzw.catpu.viewmodel.PwdChangeViewModel;

/**
 * @author : wangzw
 * @date : 20-6-1上午11:51
 * @desc :
 */
public class ChangePwdActivity extends BaseActivity {
    private ActivityPwdChangeBinding pwdChangeBinding;
    private PwdChangeViewModel viewModel;

    @Override
    public int getLayoutId() {
        return R.layout.activity_pwd_change;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        pwdChangeBinding = getBaseViewBinding();
        pwdChangeBinding.setClick(new Click());
        pwdChangeBinding.setModel(viewModel = new PwdChangeViewModel());
    }

    public class Click {
        public void back() {
            finish();
        }

        public void confirmChange() {
            if (!TextUtils.isEmpty(viewModel.getValidateError())){
                showToast(viewModel.getValidateError());
                return;
            }

            BaseRequest.getInstance().changePwd(viewModel.mAccount,
                    viewModel.mOldPaw,
                    viewModel.mNewPaw, new RegisterListener() {
                        @Override
                        public void getRespond(final BaseRespond respond) {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    int code = respond.code;

                                    showToast(respond.msg);

                                    if (code == 0){
                                        ActivityManagerUtils.getInstance().finishAllActivity();
                                        startActivity(new Intent(ChangePwdActivity.this,LoginActivity.class));
                                    }
                                }
                            });
                        }
                    });
        }


    }

}
